<template>
  <div class="crud-opts">
    <span class="crud-flex">
      <!--左侧插槽-->
      <slot name="left"/>
      <el-button icon="el-icon-plus" v-permission="permission.add==undefined ? ['']:permission.add" @click="crud.toAdd"
                 style="border-radius: 0;color:white;margin-right:10px;height:32px;background:#347DE1 !important"></el-button>
      <img class="img" src="@images/update.png" alt="" @click="doReply()">

      <!--更多操作-->
      <el-popover v-if="checkPermission(['appJobDef:edit','appJobDef:del'])" width="350" style="height:30px"
                  trigger="click" placement="bottom-start">
        <img class="img" slot="reference" src="@images/more.png" alt="">
        <div>
          <el-row v-if="checkPermission(['appJobDef:del'])">
            <el-col :span="8" style="margin-top: 5px;">
              <el-row>
                <label>常规操作</label>
              </el-row>
            </el-col>
            <el-col :span="16">
              <el-row>
                <el-button v-permission="permission.del==undefined ? ['']:permission.del"
                           :disabled="crud.selections.length == 0" type="text" :loading="crud.delAllLoading"
                           @click="toDelete(crud.selections)">删除</el-button>
              </el-row>
            </el-col>
          </el-row>
          <el-row v-if="checkPermission(['appJobDef:edit'])">
            <el-col :span="8" style="margin-top: 5px;">
              <el-row>
                <label>批量操作</label>
              </el-row>
            </el-col>
            <el-col :span="16">
              <el-row>
                <el-col :span="12">
                  <el-button :disabled="crud.selections.length == 0"
                             v-permission="permission.edit==undefined ? ['']:permission.edit" type="text"
                             @click="batchUpdateStatus(crud.selections)">修改运行状态</el-button>
                </el-col>
                <el-col :span="12">
                  <el-button :disabled="crud.selections.length == 0"
                             v-permission="permission.edit==undefined ? ['']:permission.edit" type="text"
                             @click="batchUpdateJobPeriod(crud.selections)">修改调度间隔</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-button :disabled="crud.selections.length == 0"
                             v-permission="permission.edit==undefined ? ['']:permission.edit" type="text"
                             @click="batchUpdateOffsetHour(crud.selections)">修改偏移小时</el-button>
                </el-col>
                <el-col :span="12">
                  <el-button :disabled="crud.selections.length == 0"
                             v-permission="permission.edit==undefined ? ['']:permission.edit" type="text"
                             @click="batchUpdateDateRange(crud.selections)">修改生效日期</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-button :disabled="crud.selections.length == 0"
                             v-permission="permission.edit==undefined ? ['']:permission.edit" type="text"
                             @click="batchUpdateSorNo(crud.selections)">修改优先级</el-button>
                </el-col>
                <el-col :span="12">
                  <el-button :disabled="crud.selections.length == 0"
                             v-permission="permission.edit==undefined ? ['']:permission.edit" type="text"
                             @click="batchUpdateIsOrder(crud.selections)">修改是否顺序执行</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-button :disabled="crud.selections.length == 0"
                             v-permission="permission.edit==undefined ? ['']:permission.edit" type="text"
                             @click="batchUpdateAllowErrCount(crud.selections)">修改任务出错配置</el-button>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </div>
      </el-popover>
      <!--字段筛选-->
      <el-popover placement="bottom-end" style="height:30px" width="150" trigger="click">
        <img class="img" slot="reference" src="@images/gezi.png" alt="">
        <el-checkbox v-model="allColumnsSelected" :indeterminate="allColumnsSelectedIndeterminate"
                     @change="handleCheckAllChange">
          全选
        </el-checkbox>
        <el-checkbox v-for="item in crud.props.tableColumns" :key="item.label" v-model="item.visible"
                     @change="handleCheckedTableColumnsChange(item)">
          {{ item.label }}
        </el-checkbox>
      </el-popover>

      <!--上传-->
      <img @click="crud.toExcel" v-permission="permission.import==undefined ? ['']:permission.import" class="img"
           src="@images/loaddown.png" alt="">

      <!--下载-->
      <img v-permission="permission.export==undefined ? ['']:permission.export" @click="crud.doExportByJofDef()"
           class="img" src="@images/updown.png" alt="">

      <!--条件查询-->
      <el-popover placement="bottom-start" width="888" style="height:30px" trigger="click" ref="popoverName">
        <img class="img" slot="reference" src="@images/shaixuan.png" alt="">
        <div>
          <el-form ref="form" :model="queryForm" label-position="right" label-width="100px"
                   style="max-height: 310px;overflow-y: auto;">
            <div v-for="(item, index) in moreQueryList" :key="index">
              <el-row v-if="item.type == 'textarea'">
                <el-col :span="14">
                  <el-form-item :label="item.name" :prop="item.cloumLable" class="item">
                    <el-input type="textarea" :rows="2" :placeholder="item.placeholder"
                              v-model.trim="queryForm[item.cloumLable]" style="width: 400px;"/>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row v-if="item.type == 'input'">
                <el-col :span="14">
                  <el-form-item :label="item.name" :prop="item.cloumLable" class="item">
                    <el-input v-model.trim="queryForm[item.cloumLable]" :placeholder="item.placeholder"/>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row v-if="item.type == 'select'">
                <el-col :span="14">
                  <el-form-item :label="item.name" :prop="item.cloumLable" class="item">
                    <el-select v-model="queryForm[item.cloumLable]" :placeholder="item.placeholder">
                      <el-option v-for="(ob,i) in allApp" :key="i" :label="ob.appCode" :value="ob.appCode">
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row v-if="item.type == 'radio'">
                <el-col :span="20">
                  <el-form-item :label="item.name" :prop="item.cloumLable" class="item">
                    <el-radio-group v-model="queryForm[item.cloumLable]" size="medium">
                      <el-radio-button v-for="(ob,i) in item.radioList" :key="i"
                                       :label="ob.label">{{ ob.value }}</el-radio-button>
                    </el-radio-group>
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row v-if="item.type == 'datePicker'">
                <el-col :span="14">
                  <el-form-item :label="item.name" :prop="item.cloumLable" class="item">
                    <el-date-picker v-model="queryForm[item.cloumLable]" @change="dateChange(item.cloumLable)"
                                    value-format="yyyyMMdd" type="date" placeholder="选择日期" style="width: 400px;"/>
                  </el-form-item>
                </el-col>
                <el-col :span="10" style="padding-left: 20px;line-height: 33px;">
                  <el-radio v-model="queryForm[item.cloumLableType]" @change="fastDate(item.cloumLable,index * 10 + 0)"
                            :label="index * 10 + 0">今天</el-radio>
                  <el-radio v-model="queryForm[item.cloumLableType]" @change="fastDate(item.cloumLable,index * 10 + 1)"
                            :label="index * 10 + 1">昨天</el-radio>
                  <el-radio v-model="queryForm[item.cloumLableType]" @change="fastDate(item.cloumLable,index * 10 + 2)"
                            :label="index * 10 + 2">前天</el-radio>
                </el-col>
              </el-row>

              <el-row v-if="item.type == 'dateNoTimePicker'">
                <el-col :span="14">
                  <el-form-item :label="item.name" :prop="item.cloumLable" class="item">
                    <el-date-picker v-model="queryForm[item.cloumLable]" @change="dateChange(item.cloumLable)"
                                    value-format="yyyyMMdd" type="daterange" range-separator="至"
                                    start-placeholder="开始时间" end-placeholder="结束时间" style="width: 400px;"/>
                  </el-form-item>
                </el-col>
                <el-col :span="10" style="padding-left: 20px;line-height: 33px;">
                  <el-radio v-model="queryForm[item.cloumLableType]"
                            @change="selectDateFramePicker(item.cloumLable,index * 10 + 0)"
                            :label="index * 10 + 0">本周</el-radio>
                  <el-radio v-model="queryForm[item.cloumLableType]"
                            @change="selectDateFramePicker(item.cloumLable,index * 10 + 1)"
                            :label="index * 10 + 1">本月</el-radio>
                </el-col>
              </el-row>

              <el-row v-if="item.type == 'timeFramePicker'">
                <el-col :span="14">
                  <el-form-item :label="item.name" :prop="item.cloumLable" class="item">
                    <el-date-picker v-model="queryForm[item.cloumLable]" @change="dateTimeRangeChange(item.cloumLable)"
                                    value-format="yyyy-MM-dd HH:mm:ss" type="datetimerange" range-separator="至"
                                    start-placeholder="开始时间" end-placeholder="结束时间"/>
                  </el-form-item>
                </el-col>
                <el-col :span="10" style="padding-left: 20px;line-height: 33px;">
                  <el-radio v-model="queryForm[item.cloumLableType]"
                            @change="selectTimeFramePicker(item.cloumLable,index * 10 + 0)"
                            :label="index * 10 + 0">今天</el-radio>
                  <el-radio v-model="queryForm[item.cloumLableType]"
                            @change="selectTimeFramePicker(item.cloumLable,index * 10 + 1)"
                            :label="index * 10 + 1">昨天</el-radio>
                  <el-radio v-model="queryForm[item.cloumLableType]"
                            @change="selectTimeFramePicker(item.cloumLable,index * 10 + 2)"
                            :label="index * 10 + 2">本周</el-radio>
                  <el-radio v-model="queryForm[item.cloumLableType]"
                            @change="selectTimeFramePicker(item.cloumLable,index * 10 + 3)"
                            :label="index * 10 + 3">本月</el-radio>
                </el-col>
              </el-row>
            </div>
          </el-form>
          <el-row style="border-top: 1px solid #EBEEF5;padding-top: 10px;text-align: center;">
            <el-button @click="doReset" style="width: 80px;">重置</el-button>
            <el-button type="primary" @click="doMoreQuery" style="width: 80px;">查询</el-button>
          </el-row>
        </div>
      </el-popover>
      <!--手动生成任务实例-->
      <img class="img" src="@images/next.png" alt="" @click="handleTaskInstance" title="手动生成实例">
      <!--右侧-->
      <slot name="right"/>
    </span>
    <el-button-group class="crud-opts-right">
      <el-pagination :page-size.sync="page.size" :page-sizes="[10, 50, 100, 300, 500, 1000]" :total="page.total"
                     :current-page.sync="page.page" :pager-count="5" prev-text=" " next-text=" "
                     style="margin-top: 8px;" layout="prev, pager, next,sizes,total"
                     @size-change="crud.sizeChangeHandler($event)" @current-change="crud.pageChangeHandler"/>
    </el-button-group>

    <el-dialog append-to-body title="批量修改作业运行状态" width="400px" :close-on-click-modal="false" :visible.sync="statusVisi">
      <el-form ref="form" :model="statusForm" label-width="120px">
        <div>
          <el-row>
            <el-form-item label="状态" prop="jobStatus">
              <el-select v-model="statusForm.jobStatus" filterable placeholder="请选择">
                <el-option v-for="item in this.dict.job_def_status" :key="item.value" :label="item.label"
                           :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-row>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button icon="el-icon-document-delete" @click="closeDia()">取消</el-button>
        <el-button icon="el-icon-document-add" type="primary" @click="editStatus()">确认</el-button>
      </div>
    </el-dialog>

    <el-dialog append-to-body title="批量修改作业偏移小时" width="400px" :close-on-click-modal="false"
               :visible.sync="OffsetHourVisi">
      <el-form ref="form" :model="OffsetHourForm" label-width="120px">
        <div>
          <el-row>
            <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
              <el-form-item label="偏移小时" prop="OffsetHourForm.offsetHour">
                <el-input-number v-model="OffsetHourForm.offsetHour" :min="0" :max="60" :step="1" step-strictly/>
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button icon="el-icon-document-delete" @click="closeDia()">取消</el-button>
        <el-button icon="el-icon-document-add" type="primary" @click="eaitOffset_hour()">确认</el-button>
      </div>
    </el-dialog>

    <el-dialog append-to-body title="批量修改作业调度间隔" width="400px" :close-on-click-modal="false"
               :visible.sync="jobPeriodVisi">
      <el-form ref="jobPeriodForm" :model="jobPeriodForm" :rules="jobPeriodRules" label-width="120px">
        <div>
          <el-row>
            <el-form-item label="调度间隔" prop="jobPeriod">
              <el-select v-model="jobPeriodForm.jobPeriod" filterable @change="jobPeriodChange">
                <el-option v-for="item in this.dict.cycle_job_period" :key="item.value" :label="item.label"
                           :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-row>
              <el-form-item label="日期表达式" prop="dateExpression">
                <el-input style="width: 215px" v-if="jobPeriodForm.jobPeriod =='D'"
                          v-model="jobPeriodForm.dateExpression" :disabled="true"/>
                <el-input-number v-if="jobPeriodForm.jobPeriod =='W'" v-model="jobPeriodForm.dateExpression" :min="0"
                                 :max="6"></el-input-number>
                <el-input-number v-if="jobPeriodForm.jobPeriod =='X'" v-model="jobPeriodForm.dateExpression" :min="-30"
                                 :max="30"></el-input-number>
                <el-input-number v-if="jobPeriodForm.jobPeriod =='M'" v-model="jobPeriodForm.dateExpression" :min="-30"
                                 :max="30"></el-input-number>
                <el-input-number v-if="jobPeriodForm.jobPeriod =='Q'" v-model="jobPeriodForm.dateExpression" :min="-30"
                                 :max="30"></el-input-number>
                <el-input-number v-if="jobPeriodForm.jobPeriod =='Y'" v-model="jobPeriodForm.dateExpression" :min="-365"
                                 :max="365"></el-input-number>
                <el-input style="width: 215px" v-if="jobPeriodForm.jobPeriod =='O'"
                          v-model="jobPeriodForm.dateExpression" placeholder="* * *"/>
              </el-form-item>
            </el-row>
          </el-row>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button icon="el-icon-document-delete" @click="closeDia()">取消</el-button>
        <el-button icon="el-icon-document-add" type="primary" @click="editJobPeriod">确认</el-button>
      </div>
    </el-dialog>

    <el-dialog append-to-body title="批量修改作业/任务优先级" width="400px" :close-on-click-modal="false"
               :visible.sync="sortNoVisi">
      <el-form ref="form" :model="this.sortNoForm" label-width="120px">
        <div>
          <el-row>
            <el-form-item label="优先级" prop="sortNo">
              <el-select v-model="sortNoForm.sortNo" filterable placeholder="请选择">
                <el-option v-for="item in this.dict.priority_level" :key="item.value" :label="item.label"
                           :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-row>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button icon="el-icon-document-delete" @click="closeDia()">取消</el-button>
        <el-button icon="el-icon-document-add" type="primary" @click="eaitsort_no()">确认</el-button>
      </div>
    </el-dialog>

    <el-dialog append-to-body title="批量修改作业生效日期" width="600px" :close-on-click-modal="false"
               :visible.sync="dateRangeVisi">
      <el-form ref="dateRangeForm" :model="dateRangeForm" :rules="dateRangeRules" label-width="120px">
        <div>
          <el-row>
            <el-form-item label="设置生效日期" prop="isValidDate">
              <el-switch v-model="dateRangeForm.isValidDate" active-value="1" inactive-value="2">
              </el-switch>
            </el-form-item>
          </el-row>
          <el-collapse-transition>
            <el-row v-if="dateRangeForm.isValidDate==1">
              <el-form-item label="日期区间" prop="dateRange">
                <el-date-picker v-model="dateRangeForm.dateRange" type="daterange" align="right" unlink-panels
                                range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"
                                value-format="yyyy-MM-dd">
                </el-date-picker>
              </el-form-item>
            </el-row>
          </el-collapse-transition>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button icon="el-icon-document-delete" @click="closeDia()">取消</el-button>
        <el-button icon="el-icon-document-add" type="primary" @click="editJobDateRange()">确认</el-button>
      </div>
    </el-dialog>

    <el-dialog append-to-body title="批量修改任务是否顺序执行" width="400px" :close-on-click-modal="false"
               :visible.sync="isOrderVisi">
      <el-form ref="isOrderForm" :model="isOrderForm" label-width="120px">
        <div>
          <el-row>
            <el-form-item label="是否顺序执行" prop="isOrder">
              <el-switch v-model="isOrderForm.isOrder" active-value="1" inactive-value="2"></el-switch>
            </el-form-item>
          </el-row>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button icon="el-icon-document-delete" @click="closeDia()">取消</el-button>
        <el-button icon="el-icon-document-add" type="primary" @click="editIsOrder()">确认</el-button>
      </div>
    </el-dialog>

    <el-dialog append-to-body title="批量修改任务允许出错次数" width="400px" :close-on-click-modal="false"
               :visible.sync="allowErrCountVisi">
      <el-form ref="allowErrCountForm" :model="allowErrCountForm" label-width="120px">
        <div>
          <el-row>
            <el-form-item label="允许出错次数" prop="allowErrCount">
              <el-input-number v-model="allowErrCountForm.allowErrCount" :min="0" :max="100" :step="1" step-strictly/>
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item label="出错间隔时长(秒)" prop="errDifferDuration">
              <el-input-number v-model="allowErrCountForm.errDifferDuration" :min="0" :max="1000000" :step="1"
                               step-strictly/>
            </el-form-item>
          </el-row>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button icon="el-icon-document-delete" @click="closeDia()">取消</el-button>
        <el-button icon="el-icon-document-add" type="primary" @click="editAllowErrCount()">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import jobDef, {generateTaskInstance} from '@/api/cycleJob/job_def'
import taskDef from '@/api/cycleJob/task'
import CRUD, {crud, pagination} from '@crud/crud'
import checkPermission from '@/utils/permission' // 权限判断函数

export default {
  mixins: [crud(), pagination()],
  props: {
    permission: {
      type: Object,
      default: null
    },
    moreQueryList: {
      type: Array,
      default: null
    },
    allApp: null
  },
  dicts: ['job_def_status', 'cycle_job_period', 'priority_level'],
  data() {
    var validDateRange = (rule, value, callback) => {
      if (1 == this.dateRangeForm.isValidDate && null == this.dateRangeForm.dateRange) {
        callback(new Error('日期范围不能为空！'));
      } else {
        callback();
      }
    }
    var validateDateExpression = (rule, value, callback) => {
      if (this.jobPeriodForm.jobPeriod == 'O') {
        let value = this.jobPeriodForm.dateExpression;
        if (value == '' || value == undefined || value == null) {
          callback(new Error('日期表达式不能为空！'));
        }
        const FloatRegex = /^[*0-9,-]* [*0-9,-]* [*0-9,-]*$/;
        if (!FloatRegex.test(value)) {
          callback(new Error('自定义的日期表达式格式不正确!，说明* 3,9-12 1-5,30（年 月 日）'));
        } else {
          callback();
        }
      } else {
        callback();
      }
    }
    return {
      defaultQueryForm: {
        bizDate: '',
        jobCode: null,
        jobName: null,
        jobStatus: '',
        bz: null,
        startTime: [],
        endTime: [],
        jobCodes: null
      },
      statusVisi: false,
      OffsetHourVisi: false,
      sortNoVisi: false,
      jobPeriodVisi: false,
      dateRangeVisi: false,
      isOrderVisi: false,
      allowErrCountVisi: false,
      statusForm: {jobStatus: "0"},
      OffsetHourForm: {offsetHour: "0"},
      sortNoForm: {sortNo: '1'},
      jobPeriodForm: {jobPeriod: 'D', dateExpression: null},
      dateRangeForm: {isValidDate: 1, dateRange: null},
      isOrderForm: {isOrder: '1'},
      allowErrCountForm: {allowErrCount: 0, errDifferDuration: 0},
      allColumnsSelected: true,
      allColumnsSelectedIndeterminate: false,
      qryParam: '',
      qryCloum: '',
      selectionList: [],
      jobCodes: [],
      historykey: '',
      historyvalue: '',
      queryForm: {},
      dateRangeRules: {
        dateRange: [
          {validator: validDateRange, trigger: 'blur'},
        ]
      },
      jobPeriodRules: {
        dateExpression: [
          {validator: validateDateExpression, trigger: 'blur'},
        ]
      }
    }
  },
  created() {
    this.crud.updateProp('searchToggle', true)
    if (this.crud.params.bizDate == '' || this.crud.params.bizDate) {
      this.queryForm = this.crud.params
    } else {
      this.queryForm = JSON.parse(JSON.stringify(this.defaultQueryForm));
    }
  },
  methods: {
    handleTaskInstance() {
      this.$confirm('确认生成任务实例?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        generateTaskInstance();
        this.$notify({
          title: '成功',
          message: '任务实例正在生成!',
          type: 'success'
        })
      })
    },
    checkPermission,
    toDelete(datas) {
      this.$confirm(`确认删除选中的${datas.length}条数据?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.crud.delAllLoading = true
        this.crud.doDeleteByPkId(datas)
      }).catch(() => {
      })
    },
    handleCheckAllChange(val) {
      if (val === false) {
        this.$message({
          message: '请至少选择一列',
          type: 'warning'
        });
        this.allColumnsSelected = true
        return
      }
      for (const key in this.crud.props.tableColumns) {
        this.crud.props.tableColumns[key].visible = val
      }
      this.allColumnsSelected = val
      this.allColumnsSelectedIndeterminate = false
    },
    handleCheckedTableColumnsChange(item) {
      let totalCount = 0
      let selectedCount = 0
      for (const key in this.crud.props.tableColumns) {
        ++totalCount
        selectedCount += this.crud.props.tableColumns[key].visible ? 1 : 0
      }
      if (selectedCount === 0) {
        this.crud.notify('请至少选择一列', CRUD.NOTIFICATION_TYPE.WARNING)
        this.$nextTick(function () {
          item.visible = true
        })
        return
      }
      this.allColumnsSelected = selectedCount === totalCount
      this.allColumnsSelectedIndeterminate = selectedCount !== totalCount && selectedCount !== 0
    },
    toggleSearch() {
      this.crud.props.searchToggle = !this.crud.props.searchToggle
      if (this.crud.props.searchToggle) {
        delete this.crud.query[this.qryCloum];
      }
    },
    paramChange(value) {
      this.qryParam = value;
    },
    cloumChange(value) {
      this.qryCloum = value;
      for (let key in this.crud.query) {
        if (key != this.qryCloum) {
          delete this.crud.query[key]
        }
      }
    },
    doReply() {
      this.crud.findVM('presenter').$refs['table'].clearFilter()
      this.crud.query = {}
      this.qryParam = ''
      this.qryCloum = ''
      this.doReset()
    },
    doQuery() {
      var key = "";
      var value = "";
      if (this.qryCloum == null || this.qryCloum == '') {
        this.$message({
          message: '请先选择搜索项',
          type: 'warning'
        });
        return;
      }
      if (this.qryCloum != null && this.qryCloum != '') {
        key = this.qryCloum;
        this.historykey = this.qryCloum;
      } else {
        this.historykey = "";
      }
      if (this.qryParam != '' && this.qryParam != null) {
        value = this.qryParam;
        this.historyvalue = this.qryParam;
      } else {
        this.historyvalue = "";
      }

      this.crud.query[key] = value;
      this.crud.toQuery();
    },
    jobPeriodChange() {
      if ("D" == this.jobPeriodForm.jobPeriod) {
        this.jobPeriodForm.dateExpression = null;
      } else if ("W" == this.jobPeriodForm.jobPeriod) {
        this.jobPeriodForm.dateExpression = 1;
      } else if ("X" == this.jobPeriodForm.jobPeriod) {
        this.jobPeriodForm.dateExpression = 1;
      } else if ("M" == this.jobPeriodForm.jobPeriod) {
        this.jobPeriodForm.dateExpression = 1;
      } else if ("Q" == this.jobPeriodForm.jobPeriod) {
        this.jobPeriodForm.dateExpression = 1;
      } else if ("Y" == this.jobPeriodForm.jobPeriod) {
        this.jobPeriodForm.dateExpression = 1;
      } else if ("O" == this.jobPeriodForm.jobPeriod) {
        this.jobPeriodForm.dateExpression = null;
      }
    },
    batchUpdateStatus(param) {
      this.selectionList = [];
      param.forEach((td) => {
        this.selectionList.push(td.pkId)
      });
      this.statusVisi = true;
    },
    closeDia() {
      this.isOrderVisi = false;
      this.allowErrCountVisi = false;
      this.statusVisi = false;
      this.OffsetHourVisi = false;
      this.sortNoVisi = false;
      this.jobPeriodVisi = false;
      this.dateRangeVisi = false;
    },
    editStatus() {
      let param = {"selectionList": this.selectionList, "jobStatus": this.statusForm.jobStatus}
      jobDef.batchUpdateStatus(param).then(res => {
        if (res) {
          this.statusVisi = false;
          this.$notify({
            title: '修改成功',
            type: 'success',
            duration: 2500
          })
          this.crud.refresh();
          this.statusForm.jobStatus = "0"
        }
      })
    },
    batchUpdateOffsetHour(param) {
      this.selectionList = [];
      param.forEach((td) => {
        this.selectionList.push(td.pkId)
      });
      this.OffsetHourVisi = true;
    },
    eaitOffset_hour() {
      let param = {"selectionList": this.selectionList, "offsetHour": this.OffsetHourForm.offsetHour}
      jobDef.batchUpdateOffsetHour(param).then(res => {
        if (res) {
          this.OffsetHourVisi = false;
          this.$notify({
            title: '修改成功',
            type: 'success',
            duration: 2500
          })
          this.crud.refresh();
        }
      })
    },
    batchUpdateSorNo(param) {
      this.selectionList = [];
      this.jobCodes = [];
      param.forEach((td) => {
        this.selectionList.push(td.pkId);
        this.jobCodes.push(td.jobCode);
      });
      this.sortNoVisi = true;
    },
    eaitsort_no() {
      let param = {"selectionList": this.selectionList, "jobCodeList": this.jobCodes, "sortNo": this.sortNoForm.sortNo}
      jobDef.batchUpdateSortNo(param).then(res => {
        if (res) {
          this.sortNoVisi = false;
          this.$notify({
            title: '修改成功',
            type: 'success',
            duration: 2500
          })
          this.crud.refresh();
        }
      })
    },
    batchUpdateJobPeriod(param) {
      this.selectionList = [];
      param.forEach((td) => {
        this.selectionList.push(td.pkId)
      });
      this.jobPeriodVisi = true;
    },
    editJobPeriod() {
      this.$refs['jobPeriodForm'].validate((valid) => {
        if (valid) {
          let param = {
            "selectionList": this.selectionList,
            "jobPeriod": this.jobPeriodForm.jobPeriod,
            "dateExpression": this.jobPeriodForm.dateExpression
          }
          jobDef.batchUpdateJobPeriod(param).then(res => {
            if (res) {
              this.jobPeriodVisi = false;
              this.$notify({title: '修改成功', type: 'success', duration: 2500})
              this.crud.refresh();
              this.jobPeriodForm.jobPeriod = "D"
              this.jobPeriodForm.dateExpression = null
            }
          })
        } else {
          return false;
        }
      })

    },
    batchUpdateDateRange(param) {
      this.selectionList = [];
      param.forEach((td) => {
        this.selectionList.push(td.pkId)
      });
      this.dateRangeVisi = true;
    },
    editJobDateRange() {
      this.$refs['dateRangeForm'].validate((valid) => {
        if (valid) {
          let param = {
            "selectionList": this.selectionList,
            "isValidDate": this.dateRangeForm.isValidDate,
            "dateRange": this.dateRangeForm.dateRange
          }
          jobDef.batchUpdateDateRange(param).then(res => {
            if (res) {
              this.dateRangeVisi = false;
              this.$notify({title: '修改成功', type: 'success', duration: 2500})
              this.crud.refresh();
            }
          })
        } else {
          return false;
        }
      });

    },
    batchUpdateIsOrder(param) {
      this.jobCodes = [];
      param.forEach((td) => {
        this.jobCodes.push(td.jobCode);
      });
      this.isOrderVisi = true;
    },
    editIsOrder() {
      let param = {"selectionList": this.jobCodes, "isOrder": this.isOrderForm.isOrder}
      taskDef.batchUpdateTaskByJobCode(param).then(res => {
        if (res) {
          this.isOrderVisi = false;
          this.$notify({title: '修改成功', type: 'success', duration: 2500})
          this.crud.refresh();
          this.isOrderForm.isOrder = '1'
        }
      })
    },
    batchUpdateAllowErrCount(param) {
      this.jobCodes = [];
      param.forEach((td) => {
        this.jobCodes.push(td.jobCode);
      });
      this.allowErrCountVisi = true;
    },
    editAllowErrCount() {
      let param = {
        "selectionList": this.jobCodes,
        "allowErrCount": this.allowErrCountForm.allowErrCount,
        "errDifferDuration": this.allowErrCountForm.errDifferDuration
      }
      taskDef.batchUpdateTaskByJobCode(param).then(res => {
        if (res) {
          this.allowErrCountVisi = false;
          this.$notify({title: '修改成功', type: 'success', duration: 2500})
          this.crud.refresh();
        }
      })
    },
    dateChange(cloumLable) {
      let cloum = cloumLable + "Type";
      this.queryForm[cloum] = null;
      //  alert(cloumLable);
    },
    fastDate(cloum, index) {
      index = index % 10;
      this.queryForm[cloum] = this.$moment().add(-index, 'days').format('YYYYMMDD');
    },
    selectDateFramePicker(cloum, index) {
      index = index % 10;
      if (0 == index) {
        this.queryForm[cloum] = [];
        this.queryForm[cloum].push(this.$moment().startOf('isoWeek').format('YYYYMMDD'));
        this.queryForm[cloum].push(this.$moment().endOf('day').format('YYYYMMDD'));
      }
      if (1 == index) {
        this.queryForm[cloum] = [];
        this.queryForm[cloum].push(this.$moment().startOf('month').format('YYYYMMDD'));
        this.queryForm[cloum].push(this.$moment().endOf('day').format('YYYYMMDD'));
      }
    },
    dateTimeRangeChange(cloumLable) {
      let cloum = cloumLable + "Type";
      this.queryForm[cloum] = null;
    },
    selectTimeFramePicker(cloum, index) {
      index = index % 10;
      if (0 == index) {
        this.queryForm[cloum] = [];
        this.queryForm[cloum].push(this.$moment().startOf('day').format('YYYY-MM-DD HH:mm:ss'));
        this.queryForm[cloum].push(this.$moment().endOf('day').format('YYYY-MM-DD HH:mm:ss'));
      }
      if (1 == index) {
        this.queryForm[cloum] = [];
        this.queryForm[cloum].push(this.$moment().add(-1, 'days').startOf('day').format('YYYY-MM-DD HH:mm:ss'));
        this.queryForm[cloum].push(this.$moment().add(-1, 'days').endOf('day').format('YYYY-MM-DD HH:mm:ss'));
      }
      if (2 == index) {
        this.queryForm[cloum] = [];
        this.queryForm[cloum].push(this.$moment().startOf('isoWeek').format('YYYY-MM-DD HH:mm:ss'));
        this.queryForm[cloum].push(this.$moment().endOf('day').format('YYYY-MM-DD HH:mm:ss'));
      }
      if (3 == index) {
        this.queryForm[cloum] = [];
        this.queryForm[cloum].push(this.$moment().startOf('month').format('YYYY-MM-DD HH:mm:ss'));
        this.queryForm[cloum].push(this.$moment().endOf('day').format('YYYY-MM-DD HH:mm:ss'));
      }
    },
    doReset() {
      this.queryForm = JSON.parse(JSON.stringify(this.defaultQueryForm));
      this.doMoreQuery();
    },
    doMoreQuery() {
      this.$refs['popoverName'].doClose()
      this.crud.page.page = 1;
      this.crud.params = JSON.parse(JSON.stringify(this.queryForm));
      this.crud.toQuery();
    },
  }
}
</script>

<style lang="scss" scoped>
.crud-opts {
  padding: 6px 0;
  display: -webkit-flex;
  display: flex;
  align-items: center;
}

.crud-opts .crud-opts-right {
  margin-left: auto;
}

.crud-flex {
  display: flex;
}
</style>
